float4x4 g_mDPWorld : WORLD;
float4x4 g_mDPView  : LITVIEW;
float g_fFar  = 2048.0f;
float g_fNear = 1.0f;

struct VSDPDepthIn
{
    float3 Pos : POSITION;
};

struct PSDPDepthIn
{
    float4 Pos       : POSITION;
    float  ClipDepth : TEXCOORD1;
    float  Depth     : TEXCOORD2;
};

PSDPDepthIn DPDepthFrontVS(VSDPDepthIn In)
{
    PSDPDepthIn Out;
    Out.Pos = mul(float4(In.Pos, 1.0f), mul(g_mDPWorld,g_mDPView));
    Out.Pos /= Out.Pos.w;
    Out.Pos.z *= 1;
    float fLength = length(Out.Pos.xyz);
    Out.Pos /= fLength;
    Out.ClipDepth = Out.Pos.z;
    Out.Pos.x /= Out.Pos.z + 1.0f;
    Out.Pos.y /= Out.Pos.z + 1.0f;
    Out.Pos.z = (fLength - g_fNear) / (g_fFar - g_fNear);
    Out.Pos.w = 1.0f;
    Out.Depth = Out.Pos.z;
    return Out;
}

PSDPDepthIn DPDepthBackVS(VSDPDepthIn In)
{
    PSDPDepthIn Out;
    Out.Pos = mul(float4(In.Pos, 1.0f), mul(g_mDPWorld,g_mDPView));
    Out.Pos /= Out.Pos.w;
    Out.Pos.z *= -1;
    float fLength = length(Out.Pos.xyz);
    Out.Pos /= fLength;
    Out.ClipDepth = Out.Pos.z;
    Out.Pos.x /= Out.Pos.z + 1.0f;
    Out.Pos.y /= Out.Pos.z + 1.0f;
    Out.Pos.z = (fLength - g_fNear) / (g_fFar - g_fNear);
    Out.Pos.w = 1.0f;
    Out.Depth = Out.Pos.z;
    return Out;
}

float4 DPDepthPS(PSDPDepthIn In) : COLOR0
{
    clip(In.ClipDepth);
    return In.Depth;
}

technique DepthFront
{
	pass p0
	{
		VertexShader = compile vs_3_0 DPDepthFrontVS();
		PixelShader  = compile ps_3_0 DPDepthPS();
	}
}

technique DepthBack
{
	pass p0
	{
		CullMode = None;
		VertexShader = compile vs_3_0 DPDepthBackVS();
		PixelShader  = compile ps_3_0 DPDepthPS();
	}
}